from fastapi import APIRouter, Request

from exceptions import ServerException

router = APIRouter()


@router.get("/")
async def connect_test(request: Request):
    return {"info": "connect success", "user_ip": request.client.host}


@router.get("/exceptions")
async def list_api_exceptions():
    api_errors = list()
    for sub in ServerException.__subclasses__():
        error = dict(
            name=sub.__name__,
            code=sub.code,
        )
        if hasattr(sub, 'title'):
            error['title'] = sub.title
        api_errors.append(error)
    return api_errors
